package com.dimension.service.impl;

import com.dimension.entity.Image;
import com.dimension.entity.ImageCategory;
import com.dimension.entity.Tag;
import com.dimension.mapper.ImageMapper;
import com.dimension.service.ImageService;
import com.dimension.utils.SessionUtil;
import com.dimension.vo.ImageTag;
import com.dimension.vo.ImageTagCategory;

import java.util.ArrayList;
import java.util.List;

public class ImageServiceImpl implements ImageService {
    private ImageMapper imageMapper = SessionUtil.getMapper(ImageMapper.class);

    @Override
    public List<ImageTag> getImageTagList(Integer pageNum, Integer pageSize) {
        // 1.创建最终数据
        List<ImageTag> imageTagList = new ArrayList<>();

        // 2.调用mapper封装最终数据
        List<Image> imageList = imageMapper.getImageListWithNoCondition(pageNum, pageSize);

        for (Image image : imageList) {
            List<Tag> tagList = imageMapper.getTagList(image.getId());
            ImageTag imageTag = new ImageTag();
            imageTag.setImage(image);
            imageTag.setTagList(tagList);
            imageTagList.add(imageTag);
        }

        // 3.返回数据
        return imageTagList;
    }

    @Override
    public long getTotalWithNoCondition() {
        long total = imageMapper.getTotalWithNoCondition();

        return total;
    }

    @Override
    public void decreaseImageVotesByImageId(long imageId) {
        imageMapper.decreaseImageVotesByImageId(imageId);

    }

    @Override
    public void increaseImageCommentsByImageId(long imageId) {
        imageMapper.increaseImageCommentsByImageId(imageId);

    }

    @Override
    public List<ImageTagCategory> getImageTagCategoryList(Integer pageNum, Integer pageSize) {
        //创建最终数据
        ArrayList<ImageTagCategory> imageTagCategoryList = new ArrayList<>();

        //无条件分页查询图片表
        List<Image> imageList = imageMapper.getImageListWithoutCondition(pageNum,pageSize);

        for (Image image : imageList) {
            List<Tag> tagList = imageMapper.getTagList(image.getId());                              //查询图片对应的标签列表
            ImageCategory imageCategory = imageMapper.getImageCategoryByImageId(image.getId());     //查询图片对应的图片类别
            ImageTagCategory imageTagCategory = new ImageTagCategory();
            imageTagCategory.setImage(image);
            imageTagCategory.setTagList(tagList);
            imageTagCategory.setCategory(imageCategory);
            imageTagCategoryList.add(imageTagCategory);
        }

        return imageTagCategoryList;
    }

    @Override
    public List<ImageTagCategory> getImageTagCategoryListByKeyword(String keyword, Integer pageNum, Integer pageSize) {
        //创建最终数据
        ArrayList<ImageTagCategory> imageTagCategoryList = new ArrayList<>();

        //根据标签关键词查询图片表
        keyword = "%"+keyword+"%";
        List<Image> imageList = imageMapper.getImageListByKeyword(keyword,pageNum,pageSize);

        for (Image image : imageList) {
            List<Tag> tagList = imageMapper.getTagList(image.getId());                              //查询图片对应的标签列表
            ImageCategory imageCategory = imageMapper.getImageCategoryByImageId(image.getId());     //查询图片对应的图片类别
            ImageTagCategory imageTagCategory = new ImageTagCategory();
            imageTagCategory.setImage(image);
            imageTagCategory.setTagList(tagList);
            imageTagCategory.setCategory(imageCategory);
            imageTagCategoryList.add(imageTagCategory);
        }
        return imageTagCategoryList;
    }

    @Override
    public long getTotalByKeyword(String keyword) {
        keyword = "%"+keyword+"%";
        long total = imageMapper.getTotalByKeyword(keyword);
        return total;
    }

    @Override
    public void removeImageById(long imageId) {
        imageMapper.removeCommentByImageId(imageId);                 //删除该图片下的所有评论
        imageMapper.removeBookmarkByImageId(imageId);                //删除该图片下的所有收藏信息
        imageMapper.removeImageRatingByImageId(imageId);             //删除该图片下的所有评分信息
        imageMapper.removeImageTagsRelationByImageId(imageId);       //删除图片标签关系表中关于该图片的记录
        imageMapper.removeWatchHistoryByImageId(imageId);            //删除该图片下的所有浏览历史信息
        imageMapper.removeImageById(imageId);                        //删除该图片
    }



    @Override
    public List<ImageTag> getImageTagListWithCondition(String keyword, Integer pageNum, Integer pageSize) {

        List<ImageTag> imageTagList = new ArrayList<>();
        // 2. 调用mapper获取图片列表
        List<Image> imageList = imageMapper.getImageListWithCondition(keyword, pageNum, pageSize);

        // 3. 遍历图片列表，获取每张图片的标签列表
        for (Image image : imageList) {
            if (image != null) {
                List<Tag> tagList = imageMapper.getTagListWithCondition(image.getId());
                // 4. 创建ImageTag对象并设置图片和标签列表
                ImageTag imageTag = new ImageTag();
                imageTag.setImage(image);
                imageTag.setTagList(tagList);

                // 5. 将ImageTag对象添加到最终数据列表
                imageTagList.add(imageTag);
            }
        }

        // 6. 返回最终数据列表
        return imageTagList;
    }

    @Override
    public long getTotalWithCondition(String keyword) {
        long total = imageMapper.getTotalWithCondition(keyword);
        return total;
    }

    @Override
    public List<ImageTag> getImageListWithCategoryId(Integer categoryId, Integer pageNum, Integer pageSize) {
        List<ImageTag> imageTagList = new ArrayList<>();

        // 2. 调用mapper获取推荐图片列表
        List<Image> imageList = imageMapper.getImageListWithCategoryId(categoryId,pageNum,pageSize);

        // 3. 遍历图片列表，获取每张图片的标签列表
        for (Image image : imageList) {
            if (image != null) {
                List<Tag> tagList = imageMapper.getTagListWithCategoryId(image.getId());

                // 4. 创建ImageTag对象并设置图片和标签列表
                ImageTag imageTag = new ImageTag();
                imageTag.setImage(image);
                imageTag.setTagList(tagList);

                // 5. 将ImageTag对象添加到最终数据列表
                imageTagList.add(imageTag);
            }
        }

        // 6. 返回最终数据列表
        return imageTagList;
    }

    @Override
    public ImageTag GetImageDetailById(long imageId) {

        ImageTag imageTag = new ImageTag();

        // 根据图像ID获取图像信息
        Image image = imageMapper.getImageById(imageId);
        if (image != null) {
            // 根据图像ID获取关联的标签列表
            List<Tag> tagList = imageMapper.getTagListByImageId(image.getId());

            // 将图像和标签列表设置到 ImageTag 对象中
            imageTag.setImage(image);
            imageTag.setTagList(tagList);
        }

        return imageTag;

    }

    @Override
    public List<ImageTag> getRecommendImageList(Integer categoryId) {
        List<ImageTag> imageTagList = new ArrayList<>();

        // 2. 调用mapper获取推荐图片列表
        List<Image> imageList = imageMapper.getRecommendImageList(categoryId);

        // 3. 遍历图片列表，获取每张图片的标签列表
        for (Image image : imageList) {
            if (image != null) {
                List<Tag> tagList = imageMapper.getRecommendTagListByImageId(image.getId());

                // 4. 创建ImageTag对象并设置图片和标签列表
                ImageTag imageTag = new ImageTag();
                imageTag.setImage(image);
                imageTag.setTagList(tagList);

                // 5. 将ImageTag对象添加到最终数据列表
                imageTagList.add(imageTag);
            }
        }

        // 6. 返回最终数据列表
        return imageTagList;


    }

    @Override
    public void increaseImageViewsIncrement(long imageId) {
        imageMapper.increaseImageViewsIncrement(imageId);
    }

    @Override
    public void increaseImageVotesIncrement(long imageId) {
        imageMapper.increaseImageVotesIncrement(imageId);
    }

    @Override
    public void updateImageTagsAndCategory(Long categoryId, List<Integer> tagIds, Long imageId) {
        imageMapper.updateImageCategoryByImageId(imageId,categoryId);        //修改图片的分类

        imageMapper.removeImageTagsRelationByImageId(imageId);               //删除图片标签关系表中关于该图片的记录
        for (Integer tagId : tagIds) {
            imageMapper.insertImageTagRelationByImageId(imageId,tagId);     //向图片标签关系中插入数据
        }

    }

}
